/*============================================================================*/
/*                   CONTINENTAL AUTOMOTIVE SYSTEMS                           */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* name:            APP_WindowLifter.c
* version:         1.0
* created_by:      Eunice A Ibarra Galaviz
* date_created:    June 22 2011
*=============================================================================*/
/* DESCRIPTION : Source file for Application layer for WINDOW LIFTER          */
/*============================================================================*/
/* FUNCTION COMMENT : Describes the functions for Application Layer for       */
/* WINDOW LIFTER                                                              */
/* Include the state machine for WINDOW LIFTER                                */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.0      | 22/jun/2011 |                               | Eunice Ibarra    */
/* Initial version for the Ale Window Lifter using Freescale Bolero micro.    */                                                              
/*============================================================================*/

#include "APP_WindowLifter.h"

#include "sysinit.h"

#include "HAL_Led.h"
#include "HAL_Button.h"


static T_UBYTE rub_SMcurrentState;

static T_UBYTE rub_BlockInit = FALSE;
static T_UBYTE rub_CloseInit = FALSE;
static T_UBYTE rub_OpenInit = FALSE;


void APP_WindowLifter_Init(void)
{
	HAL_Led_Init();
	HAL_Button_Init();
	
	/* Initialize WINDOW LIFTER state machine */
	APP_WindowLifter_Sm(re_STILL);
	rub_SMcurrentState = re_STILL;
}

void APP_WindowLifter_Sm(E_SM_WINDOWLIFTER le_State)
{
	switch(le_State)
	{
		case re_STILL:			
		{	
			APP_WindowLifter_Still();
			rub_SMcurrentState = re_STILL;
			break;
		}
		case re_CLOSE:			
		{
			APP_WindowLifter_Close();
			rub_SMcurrentState = re_CLOSE;
			break;
		}
		case re_CLOSE_AUTO:			
		{	
			APP_WindowLifter_Close();
			rub_SMcurrentState = re_CLOSE_AUTO;
			break;
		}
		case re_OPEN:			
		{
			APP_WindowLifter_Open();
			rub_SMcurrentState = re_OPEN;
			break;
		}
		case re_OPEN_AUTO:			
		{
			APP_WindowLifter_Open();
			rub_SMcurrentState = re_OPEN_AUTO;
			break;
		}
		case re_BLOCKED:			
		{	
			APP_WindowLifter_Block();
			rub_SMcurrentState = re_BLOCKED;
			break;
		}
		default:
			break;
	}
		
}

void APP_WindowLifter_SmNextState(PTR_UBYTE lpub_SmNextState)
{
	T_UBYTE lub_Button;
	T_UBYTE lub_ClosedWindow;
	T_UBYTE lub_OpenedWindow;
	T_UBYTE lub_Timer5s;
	
	/* By default will have the current state */
	*lpub_SmNextState = rub_SMcurrentState;
	
	/* Get the button that is been pressed */
	HAL_Button_ValidButton(&lub_Button);
	
	/* Status of the window */
	HAL_Led_IsWindowClose(&lub_ClosedWindow);
	HAL_Led_IsWindowOpen(&lub_OpenedWindow);
	
	/* Timer has counted 5s */
	HAL_Timer_HasCounted5s(&lub_Timer5s);
	
	switch(rub_SMcurrentState)
	{
		case re_STILL:			
		{	
			if(lub_Button == CONTINUOUS_UP)
			{
				*lpub_SmNextState = re_CLOSE;
			}
			else if(lub_Button == ONETOUCH_UP)
			{
				*lpub_SmNextState = re_CLOSE_AUTO;
			}
			else if(lub_Button == CONTINUOUS_DOWN)
			{
				*lpub_SmNextState = re_OPEN;
			}
			else if(lub_Button == ONETOUCH_DOWN)
			{
				*lpub_SmNextState = re_OPEN_AUTO;
			}
			
			break;
		}
		case re_CLOSE:			
		{
			if(lub_Button == ANTIPINCH)
			{
				*lpub_SmNextState = re_BLOCKED;
			}
			else if((lub_Button == INVALID) || (lub_Button == NO_ONE))
			{
				*lpub_SmNextState = re_STILL;
			}
			
			break;
		}
		case re_CLOSE_AUTO:			
		{	
			if(lub_Button == ANTIPINCH)
			{
				*lpub_SmNextState = re_BLOCKED;
			}
			if(lub_Button == CONTINUOUS_UP)
			{
				*lpub_SmNextState = re_CLOSE;
			}
			else if((lub_Button == INVALID) || (lub_ClosedWindow == TRUE) || (lub_Button == ONETOUCH_DOWN))
			{
				*lpub_SmNextState = re_STILL;
			}
			
			break;
		}
		case re_OPEN:			
		{
			if((lub_Button == INVALID) || (lub_Button == NO_ONE))
			{
				*lpub_SmNextState = re_STILL;
			}
			
			break;
		}
		case re_OPEN_AUTO:			
		{
			if(lub_Button == CONTINUOUS_DOWN)
			{
				*lpub_SmNextState = re_OPEN;
			}
			else if((lub_Button == INVALID) || (lub_OpenedWindow == TRUE) || (lub_Button == ONETOUCH_UP))
			{
				*lpub_SmNextState = re_STILL;
			}
			
			break;
		}
		case re_BLOCKED:			
		{	
			if(lub_Timer5s == TRUE)
			{
				rub_BlockInit = FALSE;				
				*lpub_SmNextState = re_STILL;
			}
			
			break;
		}
		default:
			break;
	}	
	
}


void APP_WindowLifter_Still(void)
{
	HAL_Led_WindowStill();
}


void APP_WindowLifter_Close(void)
{
	T_UBYTE lub_Led400ms;
	T_UBYTE lub_ClosedWindow;
	
	if(rub_CloseInit == TRUE)
	{
		HAL_Led_HasCounted400ms(&lub_Led400ms);
		if(lub_Led400ms == TRUE)
		{
			rub_CloseInit = FALSE;
		}
	}
	
	if(rub_CloseInit == FALSE)
	{
		HAL_Led_IsWindowClose(&lub_ClosedWindow);
		
		if(lub_ClosedWindow == FALSE)
		{
			HAL_Led_CloseNext();
			HAL_Led_Init400ms();
			rub_CloseInit = TRUE;	
		}
		
	}	
	
}


void APP_WindowLifter_Open(void)
{
	T_UBYTE lub_Led400ms;
	T_UBYTE lub_OpenedWindow;
	
	if(rub_OpenInit == TRUE)
	{
		HAL_Led_HasCounted400ms(&lub_Led400ms);
		if(lub_Led400ms == TRUE)
		{
			rub_OpenInit = FALSE;
		}
	}
	
	if(rub_OpenInit == FALSE)
	{
		HAL_Led_IsWindowOpen(&lub_OpenedWindow);
		
		if(lub_OpenedWindow == FALSE)
		{
			HAL_Led_OpenNext();
			HAL_Led_Init400ms();
			rub_OpenInit = TRUE;	
		}
		
	}
	
}


void APP_WindowLifter_Antipinch(void)
{
	
}


void APP_WindowLifter_Block(void)
{	
	T_UBYTE lub_OpenedWindow;
	
	HAL_Led_IsWindowOpen(&lub_OpenedWindow);
	
	if(lub_OpenedWindow == FALSE)
	{		
		APP_WindowLifter_Open();
	}
	else
	{
		HAL_Led_WindowStill();
	}
		
	if(rub_BlockInit == FALSE)
	{
		HAL_Timer_Init5s();
		rub_BlockInit = TRUE;
	}
	
}

